package com.ssy.lingxi.order.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.model.bo.VendorBO;
import com.ssy.lingxi.order.model.dto.VendorLogoDTO;
import com.ssy.lingxi.order.service.feign.IPlatformTemplateFeignService;
import com.ssy.lingxi.platform.template.api.feign.inner.MemberShopInnerControllerFeign;
import com.ssy.lingxi.platform.template.api.model.dto.inner.MemberShopListInnerDTO;
import com.ssy.lingxi.platform.template.api.model.dto.inner.MerchantLogoInnerDTO;
import com.ssy.lingxi.platform.template.api.model.vo.inner.MerchantLogoInnerVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺模板服务Feign接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-10-29
 */
@Service
public class PlatformTemplateFeignServiceImpl implements IPlatformTemplateFeignService {
    @Resource
    private MemberShopInnerControllerFeign memberShopInnerControllerFeign;

    /**
     * 查询供应商店铺或商城Logo
     *
     * @param shopId  商城Id
     * @param vendors 供应商列表
     * @return 查询结果
     */
    @Override
    public Wrapper<List<VendorLogoDTO>> findVendorShopLogos(Long shopId, List<VendorBO> vendors) {
        MerchantLogoInnerDTO innerDTO = new MerchantLogoInnerDTO();
        innerDTO.setShopId(shopId);
        innerDTO.setList(vendors.stream().map(vendor -> {
            MemberShopListInnerDTO member = new MemberShopListInnerDTO();
            member.setMemberId(vendor.getVendorMemberId());
            member.setRoleId(vendor.getVendorRoleId());
            return member;
        }).collect(Collectors.toList()));

        try {
            Wrapper<List<MerchantLogoInnerVO>> result = memberShopInnerControllerFeign.findMerchantLogoByShopId(innerDTO);
            if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
                return Wrapper.fail(result.getCode(), result.getMessage());
            }

            return CollectionUtils.isEmpty(result.getData()) ? Wrapper.success(new ArrayList<>()) : Wrapper.success(result.getData().stream().map(merchantLogoInnerVO ->
                    new VendorLogoDTO(shopId, merchantLogoInnerVO.getMemberId(), merchantLogoInnerVO.getRoleId(), merchantLogoInnerVO.getName(), merchantLogoInnerVO.getLogo())
            ).collect(Collectors.toList()));
        } catch (Exception e) {
            return Wrapper.fail(ResponseCode.SERVICE_PLATFORM_TEMPLATE_ERROR, e.getMessage());
        }
    }

    /**
     * 查询供应商店铺或商城Logo
     *
     * @param shopId         商城Id
     * @param vendorMemberId 供应商会员Id
     * @param vendorRoleId   供应商会员角色Id
     * @return 供应商Logo
     */
    @Override
    public Wrapper<VendorLogoDTO> findVendorShopLogo(Long shopId, Long vendorMemberId, Long vendorRoleId) {
        MerchantLogoInnerDTO innerDTO = new MerchantLogoInnerDTO();
        innerDTO.setShopId(shopId);
        MemberShopListInnerDTO member = new MemberShopListInnerDTO();
        member.setMemberId(vendorMemberId);
        member.setRoleId(vendorRoleId);
        innerDTO.setList(Collections.singletonList(member));

        try {
            Wrapper<List<MerchantLogoInnerVO>> result = memberShopInnerControllerFeign.findMerchantLogoByShopId(innerDTO);
            if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
                return Wrapper.fail(result.getCode(), result.getMessage());
            }

            if (!CollectionUtils.isEmpty(result.getData())) {
                MerchantLogoInnerVO innerVO = result.getData().stream().filter(r -> r.getMemberId().equals(vendorMemberId) && r.getRoleId().equals(vendorRoleId)).findFirst().orElse(null);
                if (innerVO != null) {
                    return Wrapper.success(new VendorLogoDTO(shopId, vendorMemberId, vendorRoleId, innerVO.getName(), innerVO.getLogo()));
                }
            }

            return Wrapper.success(new VendorLogoDTO(shopId, vendorMemberId, vendorRoleId));
        } catch (Exception e) {
            return Wrapper.fail(ResponseCode.SERVICE_PLATFORM_TEMPLATE_ERROR, e.getMessage());
        }
    }
}
